/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.LoginHistoryDAO;
import com.ivanbiz.helper.HibernateUtil;
import com.ivanbiz.model.LoginHistory;
import com.ivanbiz.model.User;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class LoginHistoryDAOImpl extends GenericDAOImpl implements LoginHistoryDAO {

    @Override
    public Object getDataByEqual(Class clazImpl, String variable1, Object input1, String variable2, Object input2) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();
            Criteria crit = session.createCriteria(clazImpl);
            crit.add(Restrictions.eq(variable1, input1));
            crit.add(Restrictions.eq(variable2, input2));
            Object obj = crit.uniqueResult();
            HibernateUtil.commitTransaction();
            return obj;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void save(LoginHistory loginHistory, User user) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.save(loginHistory);
            session.update(user);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void update(LoginHistory loginHistory, User user) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            session.update(loginHistory);
            session.update(user);
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public List<LoginHistory> getDataByEquals(Class clasImpl, String variable1, Object input1, String variable2, Object input2) throws Exception {
        try {
            Session session = HibernateUtil.getSession();
            Criteria crit = session.createCriteria(clasImpl);
            crit.add(Restrictions.eq(variable1, input1));
            crit.add(Restrictions.eq(variable2, input2));
            List list = crit.list();
            return list;
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
    }
}
